package com.liupengyuan.one.action;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;

import com.liupengyuan.framework.util.JSONOuter;
import com.liupengyuan.framework.util.TreeNode;
import com.liupengyuan.framework.web.struts2.BaseAction;
import com.liupengyuan.one.model.Tree;
import com.liupengyuan.one.service.TreeService;
import com.opensymphony.xwork2.ModelDriven;
@Controller("treeAction")
@Scope("prototype")
public class TreeAction extends BaseAction implements ModelDriven<Tree>{
	private Tree tree;
	@Autowired
	private TreeService treeService;
	
	public void tree(){
		List<Tree> list = treeService.findByparentId(tree.getId());
		if(!CollectionUtils.isEmpty(list)){
			List<TreeNode> treeList = new ArrayList<TreeNode>();
			for(Tree tree:list){
				TreeNode treeNode = new TreeNode();
				treeNode.setId(tree.getId());
				treeNode.setName(tree.getName());
				treeNode.setpId(tree.getParentNodeId());
				List<Tree> temp = treeService.findByparentId(tree.getId());
				boolean flag = true;
				if(CollectionUtils.isEmpty(temp)){
					flag = false;
				}
				treeNode.setIsParent(flag);
				if(flag){
					treeNode.setIcon("img/root1.png");
				}else{
					treeNode.setIcon("img/org.png");
				}
				
				treeList.add(treeNode);
			}
			JSONOuter.writeTreeNode(getPrintWriter(), treeList);
		}
	}
	public void save(){
		
	}
	public void update(){
		
	}
	
	public void list(){
		
	}
	public void delete(){
		
	}
	
	public Tree getModel() {
		tree = new Tree();
		return tree;
	}

}
